import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;


import javax.swing.JComponent;

/**
 * @author Anna-Katharina Wickert 
 * @author Yannic Remmet
 * @author Tobias Trabelsi
 * 
 * @version 0.99
 * @since 22.06.2012
 *
 * @license Beer-Ware License (Revision 42)
 *
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * We wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy us a beer in return.
 * ----------------------------------------------------------------------------
 */
/**
 * @author yannicremmet
 * Observer JComponent for Game of Life
 */
public class ObserverComponent extends JComponent implements MyObserver{
	private static final long serialVersionUID = 1L;
	private Logic log;
	private int wid;
	private int hei;
	
	public ObserverComponent(Logic log) {
		this.log = log;
		this.setBackground(Color.WHITE);
	}
	@Override
	public void update(ArrayList<Integer> u) {
		Graphics g = this.getGraphics();
		while(u.size() >= 2)
			drawCells(u.remove(0), u.remove(0), g);
		
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		wid = getWidth()/log.getWidth();
		hei = getHeight()/log.getHeight();
		int i = 0;
		int j = 0;
		while(i*wid < getWidth()) {
			g.drawLine(i*wid, 0, i*wid, getHeight());
			i++;
		}
		while(j*hei < getHeight()){
			g.drawLine(0,j*hei, i*wid, j*hei);
			j++;
		}
		for(i = 0; i< log.getWidth(); i++)
			for( j= 0; j< log.getHeight(); j++)
				drawCells(i, j, g);
			
		
	}
	
	void drawCells(int x, int y, Graphics g){
		if(log.isAlive(x, y)){
			g.setColor(Color.BLACK);
			g.fillRect(
					x*wid , 
					y*hei , 
					wid, 
					hei);

		}
		else{
			g.setColor(Color.WHITE);
			g.fillRect(
					x*wid +1, 
					y*hei +1, 
					wid-1, 
					hei-1);
		}	
	}
	public int getWid() {
		return wid;
	}
	public int getHei() {
		return hei;
	}

}
